export default {
  initLogic(list, baseTags) {
    let target = []
    for (let i = 0; i < list.length; i++) {
      if (list[i].labs && list[i].labs.length) {
        // let tag = baseTags.find(item => item.assetCode === list[i].labs[0].labId)
        target[i] = {
          logicalOperation: list[i].logicTyp,
          tagList: []
        }
        for (let j = 0; j < list[i].labs.length; j++) {
          if (list[i].labs[j].labs && list[i].labs[j].labs.length) {
            target[i].tagList[j] = {
              logicalOperation: list[i].labs[j].logicTyp,
              tagList: []
            }
            for (let k = 0; k < list[i].labs[j].labs.length; k++) {
              let tag = baseTags.find(item => item.assetCode === list[i].labs[j].labs[k].labId)
              target[i].tagList[j].tagList.push({
                tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
                tagNm: tag.assetNm,
                valueType: tag.valueType.toString(),
                valueTypeCode: tag.valueTypeCode,
                tagCode: list[i].labs[j].labs[k].labId,
                operationType: list[i].labs[j].labs[k].oprator,
                operationValue: list[i].labs[j].labs[k].valArray.length
                  ? list[i].labs[j].labs[k].valArray
                  : list[i].labs[j].labs[k].valLft
                  ? [list[i].labs[j].labs[k].valLft, list[i].labs[j].labs[k].valRit]
                  : list[i].labs[j].labs[k].val,
                enums: tag.enums,
                labTbl: list[i].labs[j].labs[k].labTbl,
                labFldMap: list[i].labs[j].labs[k].labFldMap,
                dictDatabaseType: list[i].labs[j].labs[k].datTyp,
                isDerLabel: list[i].labs[j].labs[k].isDerLabel || '0'
              })
            }
          } else {
            let tag = baseTags.find(item => item.assetCode === list[i].labs[j].labId)
            target[i].tagList[j] = {
              tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
              tagNm: tag.assetNm,
              valueType: tag.valueType.toString(),
              valueTypeCode: tag.valueTypeCode,
              tagCode: list[i].labs[j].labId,
              operationType: list[i].labs[j].oprator,
              operationValue: list[i].labs[j].valArray.length
                ? list[i].labs[j].valArray
                : list[i].labs[j].valLft
                ? [list[i].labs[j].valLft, list[i].labs[j].valRit]
                : list[i].labs[j].val,
              enums: tag.enums,
              labTbl: list[i].labs[j].labTbl,
              labFldMap: list[i].labs[j].labFldMap,
              dictDatabaseType: list[i].labs[j].datTyp,
              isDerLabel: list[i].labs[j].isDerLabel || '0'
            }
          }
        }
      } else {
        let tag = baseTags.find(item => item.assetCode === list[i].labId)
        target[i] = {
          tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
          tagNm: tag.assetNm,
          valueType: tag.valueType.toString(),
          valueTypeCode: tag.valueTypeCode,
          tagCode: list[i].labId,
          operationType: list[i].oprator,
          operationValue: list[i].valArray.length
            ? list[i].valArray
            : list[i].valLft
            ? [list[i].valLft, list[i].valRit]
            : list[i].val,
          enums: tag.enums,
          labTbl: list[i].labTbl,
          labFldMap: list[i].labFldMap,
          dictDatabaseType: list[i].datTyp,
          isDerLabel: list[i].isDerLabel || '0'
        }
      }
    }
    return target
  },
  initParams(formData) {
    let derivedTag = {
      request: {
        labGrps: {},
        grpFlter: []
      },
      extra: {},
      cache: {}
    }
    let tagList = formData.tagList
    if (tagList.length) {
      derivedTag.request.labGrps = {
        dataDate: '',
        labs: [],
        level: 1,
        logicTyp: formData.logicalOperation || 'and'
      }
      for (let i = 0; i < tagList.length; i++) {
        if (tagList[i].tagList) {
          derivedTag.request.labGrps.labs[i] = {
            labs: [],
            level: 2,
            logicTyp: tagList[i].logicalOperation
          }
          for (let j = 0; j < tagList[i].tagList.length; j++) {
            if (tagList[i].tagList[j].tagList) {
              derivedTag.request.labGrps.labs[i].labs[j] = {
                labs: [],
                level: 3,
                logicTyp: tagList[i].tagList[j].logicalOperation
              }
              for (let k = 0; k < tagList[i].tagList[j].tagList.length; k++) {
                var target = tagList[i].tagList[j].tagList[k]
                let item = {
                  labTbl: target.labTbl,
                  oprator: target.operationType,
                  datTyp: target.dictDatabaseType,
                  logicTyp: 'and',
                  labFldMap: target.labFldMap,
                  labId: target.tagCode,
                  valueType: target.valueType
                }
                if (target.valueType === '1') {
                  item.val = ''
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = target.operationValue
                } else if (
                  target.valueType == '4' ||
                  target.valueType == '5' ||
                  target.valueType == '6' ||
                  target.valueType == '7'
                ) {
                  if (target.operationType === 'a<=x<b') {
                    item.val = ''
                    item.valLft = target.operationValue[0]
                    item.valRit = target.operationValue[1]
                    item.valArray = []
                  } else {
                    item.val = target.operationValue
                    item.valLft = ''
                    item.valRit = ''
                    item.valArray = []
                  }
                } else {
                  item.val = target.operationValue
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = []
                }
                derivedTag.request.labGrps.labs[i].labs[j].labs[k] = item
                if (!Object.keys(derivedTag.cache).find(label => label === target.tagCode)) {
                  derivedTag.cache[target.tagCode] = {
                    tagNm: target.tagNm
                  }
                }
              }
            } else {
              var target2 = tagList[i].tagList[j]
              let item = {
                labTbl: target2.labTbl,
                oprator: target2.operationType,
                datTyp: target2.dictDatabaseType,
                logicTyp: 'and',
                labFldMap: target2.labFldMap,
                labId: target2.tagCode,
                valueType: target2.valueType
              }
              if (target2.valueType == '1') {
                item.val = ''
                item.valLft = ''
                item.valRit = ''
                item.valArray = target2.operationValue
              } else if (
                target2.valueType == '4' ||
                target2.valueType == '5' ||
                target2.valueType == '6' ||
                target2.valueType == '7'
              ) {
                if (target2.operationType === 'a<=x<b') {
                  item.val = ''
                  item.valLft = target2.operationValue[0]
                  item.valRit = target2.operationValue[1]
                  item.valArray = []
                } else {
                  item.val = target2.operationValue
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = []
                }
              } else {
                item.val = target2.operationValue
                item.valLft = ''
                item.valRit = ''
                item.valArray = []
              }
              derivedTag.request.labGrps.labs[i].labs[j] = item
              if (!Object.keys(derivedTag.cache).find(label => label === target2.tagCode)) {
                derivedTag.cache[target2.tagCode] = {
                  tagNm: target2.tagNm
                }
              }
            }
          }
        } else {
          let item = {
            labTbl: tagList[i].labTbl,
            oprator: tagList[i].operationType,
            datTyp: tagList[i].dictDatabaseType,
            logicTyp: 'and',
            labFldMap: tagList[i].labFldMap,
            labId: tagList[i].tagCode,
            valueType: tagList[i].valueType
          }
          if (tagList[i].valueType == '1') {
            item.val = ''
            item.valLft = ''
            item.valRit = ''
            item.valArray = tagList[i].operationValue
          } else if (
            tagList[i].valueType == '4' ||
            tagList[i].valueType == '5' ||
            tagList[i].valueType == '6' ||
            tagList[i].valueType == '7'
          ) {
            if (tagList[i].operationType === 'a<=x<b') {
              item.val = ''
              item.valLft = tagList[i].operationValue[0]
              item.valRit = tagList[i].operationValue[1]
              item.valArray = []
            } else {
              item.val = tagList[i].operationValue
              item.valLft = ''
              item.valRit = ''
              item.valArray = []
            }
          } else {
            item.val = tagList[i].operationValue
            item.valLft = ''
            item.valRit = ''
            item.valArray = []
          }
          derivedTag.request.labGrps.labs[i] = item
          if (!Object.keys(derivedTag.cache).find(label => label === tagList[i].tagCode)) {
            derivedTag.cache[tagList[i].tagCode] = {
              tagNm: tagList[i].tagNm
            }
          }
        }
      }
    }
    return derivedTag
  }
}
